package com.nibianplus.volley.request;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

public class CommonGetRequest  extends Request<JSONObject>{

	
	
	private Listener<JSONObject> mListener;
	
	
	
	public CommonGetRequest(String url,Listener<JSONObject> listener,
			ErrorListener errorListener) {
		super(Request.Method.GET, url, errorListener);

		mListener = listener;
	}



	@Override
	protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
		try {
			String jsonString = new String(response.data,
					HttpHeaderParser.parseCharset(response.headers));

			return Response.success(new JSONObject(jsonString),
					HttpHeaderParser.parseCacheHeaders(response));
		} catch (UnsupportedEncodingException e) {
			return Response.error(new ParseError(e));
		} catch (JSONException je) {
			return Response.error(new ParseError(je));
		}
	}



	@Override
	protected void deliverResponse(JSONObject response) {
		mListener.onResponse(response);
	}
	
}

